package com.example.demo.job;

import com.example.demo.bean.FlowBean;
import com.example.demo.bean.PhoneBean;
import com.example.demo.mapper.MyMapper;
import com.example.demo.reducer.MyReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class DemoApplication {

    //客户端代码，写完交给ResourceManager框架去执行
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, DemoApplication.class.getSimpleName());
        //打成jar执行
        job.setJarByClass(DemoApplication.class);

        //数据在哪里？
        FileInputFormat.setInputPaths(job, args[0]);
        //使用哪个mapper处理输入的数据？
        job.setMapperClass(MyMapper.class);
        //map输出的数据类型是什么？
        job.setMapOutputKeyClass(PhoneBean.class);
        job.setMapOutputValueClass(FlowBean.class);

        //使用哪个reducer处理输入的数据？
        job.setReducerClass(MyReducer.class);
        //reduce输出的数据类型是什么？
        job.setOutputKeyClass(PhoneBean.class);
        job.setOutputValueClass(FlowBean.class);

        //数据输出到哪里？
        // 设置输出路径：如果输出路径存在则循环删除
        Path outPath = new Path(args[1]);
        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(outPath)){
            fs.delete(outPath, true);
        }
        fs.close();
        FileOutputFormat.setOutputPath(job, outPath);
        job.setNumReduceTasks(2);
        //交给yarn去执行，直到执行结束才退出本程序
        job.waitForCompletion(true);
    }
}